<template>
	<view>

		<view class="main">
			<u-form :model="form" ref="uForm" :rules="rules">
				<u-form-item label="账号" labelWidth="160" prop='account'>

					<u-input v-model="form.account" placeholder='请输入账号' />


				</u-form-item>
				<u-form-item label="验证码" labelWidth="160" prop='graphicVerifyCode'>

					<view class="Captcha">
						<u-input v-model="form.graphicVerifyCode" placeholder='请输入验证码' />
						<view class="code-img-wrapper" @click="updateImageCode" style="width: 300rpx;">
							<canvas style="width:260rpx;height:90rpx;" canvas-id="canvas"></canvas>
						</view>
					</view>


				</u-form-item>
				<u-form-item label="登录密码" prop="password" labelWidth="160">
					<u-input v-model="form.password" type='password' class="form-item" placeholder="请输入密码" />
				</u-form-item>
				<u-form-item label="确认密码" prop="newpassword" labelWidth="160">
					<u-input v-model="form.newpassword" type='password' class="form-item" placeholder="请再次确认密码" />
				</u-form-item>


			</u-form>
			<view class="btn" @click="submit">注册并登录</view>



		</view>
	</view>
</template>

<script>
	import {
		Mcaptcha
	} from '../../utils/mcaptcha.js'
	export default {
		data() {
			return {
				tips: '',
				// refCode: null,
				form: {
					account: '',
					password: '',
					graphicVerifyCode: ''

				},
				rules: {
					account: [
						// 对name字段进行长度验证
						{
							required: true,
							message: "请输入账号",
							trigger: "blur"
						},
						{
							min: 1,
							message: "最少一位",
							trigger: "blur"
						}


					],
					graphicVerifyCode: [{
						required: true,
						message: "验证码不能为空",
						trigger: "blur"
					}],
					password: [{
						min: 6,
						max: 20,
						message: '密码需要6-20字数字或字母',
						trigger: 'change'
					}],
					newpassword: [{
							required: true,
							message: "确认密码不能为空",
							trigger: "blur"
						},
						{
							trigger: 'blur',
							validator: (rule, value, callback) => {
								if (value !== this.form.password) {
									callback(new Error('两次输入密码不一致'))
								} else {
									callback()
								}
							}
						}
					]

				}



			}
		},
		// 初始化

		onShow(params) {


		},
		onReady() {
				this.$refs.uForm.setRules(this.rules);
			this.mcaptcha = new Mcaptcha({
				el: 'canvas',
				width: 80,
				height: 35,
				createCodeImg: ""
			});
		},

		methods: {
			// 刷新验证码
			updateImageCode() {
				this.mcaptcha.refresh()
			},
			codeChange(text) {
				console.log("触发了吗")
				this.tips = text;
			},

			submit() {
				console.log("提交")
				if (!this.form.graphicVerifyCode) {
					return uni.showToast({
						title: '请输入图形验证码'
					})
				}
				let validate = this.mcaptcha.validate(this.form.graphicVerifyCode)
				if (!validate) {
					return uni.showToast({
						title: '图形验证码错误'
					})
				}
				this.$refs.uForm.validate(valid => {
					if (valid) {

						console.log('form表单数据', this.form)
						const requestData = {
							account: this.form.account,
							password:this.form.password
						};
						console.log('发送的数据', requestData)
						this.$http.LoginHttp.login(
							requestData
						).then(response => {
            // 这里处理响应
            console.log('Response:', response);
			if(response.code==200){
				uni.$u.toast(response.message);
				uni.setStorageSync("token",response.token)
				uni.setStorageSync("userId",response.userId)
				uni.setStorageSync("account",response.account)
				 setTimeout(() => {
					 uni.switchTab({
					 url: '../mine/mine'
					 })
				       
				    }, 1500);
			}
        })
        .catch(error => {
            // 这里处理错误
            console.log('Error:', error);
        });;

					}
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.loginImg {
		width: 100%;
		height: 512rpx;
	}

	.main {
		width: 750rpx;
		height: 1136rpx;
		background: #FFF;
		border-radius: 32rpx 32rpx 0rpx 0rpx;
		opacity: 1;
		position: relative;
		top: -30rpx;
		padding: 50rpx 80rpx;
	}

	.yzm {
		display: flex;

	}

	.btn {
		margin: 0 auto;
		width: 560rpx;
		height: 96rpx;
		background: #0FE3BD;
		border-radius: 80rpx 80rpx 80rpx 80rpx;
		opacity: 0.5;
		text-align: center;
		line-height: 96rpx;
		color: #FFF;
		margin-top: 40rpx;
		font-size: 34rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.gx {
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;

	}

	.yhtl {
		margin-top: 30rpx;
		text-align: center;
	}

	.choose {
		width: 24rpx;
		height: 24rpx;
		vertical-align: middle;
	}

	.country {
		display: flex;
	}

	.col-g {
		color: #0FE3BD;
		;
	}

	.Captcha {
		width: 100%;
		display: flex;
		justify-content: space-between;
	}
</style>